const API = require("../../http/API")
const http = require("../../http/http")
Page({
  data: {
    title: ['最新', '入门', '进阶', '免费', '图文', '兑换', '专题'],
    sortList: '',
    isshows: false,
    // 保存数据
    kecheng: '',
    // 页数
    pagenum: 1,
    // 触底加载谁
    chudi: false,
    // 保存的id
    id: null,
    color: false,
    // 关键字
    keyword: '',
    text: "更多筛选"
  },
  isshow() {
    if (this.data.text==="收起") {
      this.setData({
        text: "更多筛选",
      })
    } else {
      this.setData({
        text: "收起",
      })
    }
    this.setData({
      isshows: !this.data.isshows,
    })
  },
  // 搜索
  keywordsChange(e) {
    if (!e.detail.value) {
      this.setData({
        keyword: ''
      })
    }
    this.setData({
      pagenum: 1,
      keyword: e.detail.value
    })
    if (this.data.id) {
      http(API.courses, {
        type: 1,
        page: this.data.pagenum,
        limit: 10,
        title: this.data.keyword,
        course_subjects_ids: this.data.id
      }).then(res => {
        this.setData({
          kecheng: res.data.data
        })
      })
    } else {
      http(API.courses, {
        type: 1,
        page: this.data.pagenum,
        limit: 10,
        title: this.data.keyword,
      }).then(res => {
        this.setData({kecheng: res.data.data})
      })
    }
  },
  quanbu() {
    this.setData({
      chudi: false,
      pagenum: 1,
      color: false,
      id: ''
    })
    http(API.courses, {
      type: 1,
      page: 1,
      limit: 10,
      title: this.data.keyword ? this.data.keyword : '',
    }).then(res => {
      this.setData({
        kecheng: res.data.data
      })
    })
  },
  id(options) {
    this.setData({
      id: options.currentTarget.dataset.id,
      chudi: true,
      pagenum: 1,
      color: options.currentTarget.dataset.id
    })
    http(API.courses, {
      type: 1,
      page: this.data.pagenum,
      limit: 10,
      course_subjects_ids: options.currentTarget.dataset.id,
      title: this.data.keyword ? this.data.keyword : ''
    }).then(res => {
      this.setData({
        kecheng: res.data.data
      })
    })
  },
  onLoad(options) {
    http(API.sortList).then(res => {
      this.setData({
        sortList: res.data.course_subjects
      })
    })
    http(API.courses, {
      type: 1,
      page: this.data.pagenum,
      limit: 10,
    }).then(res => {
      this.setData({
        kecheng: res.data.data
      })
    })
  },
  // 触底加载
  onReachBottom: function () {
    if (this.data.chudi === false) {
      http(API.courses, {
        type: 1,
        page: this.data.pagenum += 1,
        limit: 10,
        title: this.data.keyword ? this.data.keyword : '',
      }).then(res => {
        this.setData({
          kecheng: [...this.data.kecheng, ...res.data.data]
        })
        if (res.data.data == false) {
          wx.showToast({
            title: '没有更多的数据了',
            icon: 'none'
          })
        }
      })
    }
    if (this.data.id) {
      http(API.courses, {
        type: 1,
        page: this.data.pagenum += 1,
        limit: 10,
        course_subjects_ids: this.data.id,
        title: this.data.keyword ? this.data.keyword : ''
      }).then(res => {
        this.setData({
          kecheng: [...this.data.kecheng, ...res.data.data]
        })
        if (res.data.data == false) {
          wx.showToast({
            title: '没有更多的数据了',
            icon: 'none'
          })
        }
      })
    }
  },
})